package org.example.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.example.mapper.UserControlMapper;
import org.example.pojo.Elderly;
import org.example.pojo.Family;
import org.example.pojo.UpdatePassword;
import org.example.pojo.Volunteer;
import org.example.service.UserControlService;
import org.example.utils.PageBean;
import org.example.utils.R;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 实现层
 * @Author:曹震
 * @Date: 2024/11/29 19:15
 */
@Service
public class UserControlServiceImpl implements UserControlService {
    @Resource
    private UserControlMapper mapper;

    @Override
    public R registerElderly(Elderly elderly) {
        String elderlyId ="E"+ elderly.getElderlyId();
        System.out.println(elderly.getElderlyName());
        elderly.setElderlyId(elderlyId);
        int i = mapper.insertElderly(elderly);
        if (i<=0){
            return  R.error("注册失败",null);
        }else {
            return R.successful("注册成功", i);
        }
    }
    @Override
    public R setPassword(UpdatePassword updatePassword) {
        if (updatePassword.getOldPassword().equals(updatePassword.getNewPassword())){
            return R.error("你两次输入的密码不相同", null);
        }
        String userType = String.valueOf(updatePassword.getUserId().charAt(0));
        if (userType.equals("E")){
            userType="elderly";
        }else if (userType.equals("V")){
            userType="volunteer";
        }else if (userType.equals("J")){
            userType="family";
        } else if (userType.equals("A")) {
            userType="community";
        }
        int i = mapper.setPassword(userType,updatePassword.getUserId(),updatePassword.getNewPassword());
        if (i<=0){
            return R.error("用户账号格式错误或输入错误，或者旧密码错误", null);
        }else {
            return R.successful("修改成功", null);
        }

    }

    @Override
    public R findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Volunteer> volunteers = mapper.findVolunteer();
        if (volunteers.isEmpty()){
            return R.empty("没有数据",null);
        }
        PageInfo<Volunteer> volunteerPageInfo = new PageInfo<>(volunteers);

        PageBean<Volunteer> objectPageBean = new PageBean<>(
                currentPage,
                pageSize,
                volunteerPageInfo.getTotal(),
                volunteerPageInfo.getList()
        );
        return R.successful("查询成功",objectPageBean);
    }

    @Override
    public R logining(String communityId, String passWord) {
        int message = mapper.findAdmin(communityId,passWord);
        System.out.println(communityId+passWord);
        System.out.println(message);
        if (message == 0){
            return R.error("没有该数据", null);
        }else {
            return R.successful("找到该数据", null);
        }
    }
    //查询所有志愿者信息，根据名字
    @Override
    public R selectAll(int currentPage ,int pageSize,String volunteerName) {
        PageHelper.startPage(currentPage,pageSize);
        List<Volunteer> volunteers = mapper.selectAllVolunteer(volunteerName);
        if (volunteers.isEmpty()){
            return R.empty("data null" ,null);
        }
        PageInfo<Volunteer> volunteerPageInfo = new PageInfo<>(volunteers);
        PageBean<Volunteer> volunteerPageBean = new PageBean<>(currentPage, pageSize, volunteerPageInfo.getTotal(), volunteerPageInfo.getList());
        return R.successful("查询成功",volunteerPageBean);
    }
//根据id得到志愿者相关信息
    @Override
    public R selectAll(String volunteersId) {
        Volunteer volunteerById = mapper.getVolunteerById(volunteersId);
        if (volunteerById == null){
            return R.empty("没有找到相关数据",null);
        }
        return  R.successful("查询成功",volunteerById);
    }
//拒绝需要理由
    @Override
    public int refuseById(String volunteersId ,String objection) {
        int refuse = mapper.refuse(volunteersId,objection);
        return refuse;
    }

    @Override
    public int passVolunteerById(String volunteersId) {
        int pass = mapper.pass(volunteersId);
        return pass;
    }

    @Override
    public R findByIdCard(String query) {
        List<Elderly> byIdCard = mapper.findByIdCard(query);
        if (byIdCard.isEmpty()){
            return R.empty("为空",null);
        }else {
            return R.successful("成功",byIdCard);
        }

    }

    @Override
    public R findByName(String query) {
        List<Elderly> byName = mapper.findByName(query);
        if (byName.isEmpty()){
            return R.empty("为空",null);
        }else {
            return R.successful("成功",byName);
        }

    }

    @Override
    public R selectElderlyAll() {

       List<Elderly> allElderly = mapper.selectElderlyAll();
       if (allElderly.isEmpty()){
           return R.error("失败",null);
       }
        return R.successful("成功",allElderly);
    }
//根据角色id，查询相关消息
    @Override
    public R searchPerson(String userId) {
        if (userId.startsWith("V")){
            Volunteer volunteer = mapper.volunteerRole(userId);
            return R.successful("successful",volunteer);
        }
        else if (userId.startsWith("E")){
            Elderly elderly = mapper.elderlyRole(userId);
            return R.successful("successful",elderly);
        }
        else if ((userId.startsWith("F"))){
            Family family = mapper.familyRole(userId);
            return R.successful("successful",family);
        } else {
            return R.error("发现错误",null);
        }

    }


}
